# -*- Makefile -*-

CC=gcc
LD=gcc
CFLAGS  =-Wall `sdl-config --cflags`
CFLAGS +=-O9 -funroll-loops -fomit-frame-pointer
LDFLAGS=`sdl-config --libs` -lSDL_mixer

ifdef DEBUG
CFLAGS +=-g -DDEBUG -D_DEBUG
endif

C_FILES=src/dlb.c src/linked.c src/sprite.c src/ag_core.c src/ag.c
OBJ_FILES=src/dlb.o src/linked.o src/sprite.o src/ag_core.o src/ag.o
TEST_OBJS=$(OBJ_FILES:src/ag.o=src/ag_test.o)

all:ag

ag: $(OBJ_FILES)
	$(LD) $(LDFLAGS) -o $@ $^

ag_test: $(TEST_OBJS)
	$(LD) $(LDFLAGS) -o $@ $^

%.o: %.c
	$(CC) $(CFLAGS) -c -o $@ $^

src/ag_test.o: src/ag_test.c
	$(CC) $(CFLAGS) -DUNIT_TEST -c -o $@ $^
src/agcore_test.o: src/ag_core.c
	$(CC) $(CFLAGS) -DUNIT_TEST -c -o $@ $^

tests: test_ag test_agcore test_linked test_dlb

test_ag: ag $(TEST_OBJS) src/unittest.h
	$(LD) $(LDFLAGS) -o $@ $(TEST_OBJS)

test_agcore: src/agcore_test.o src/dlb.o src/linked.o src/unittest.h
	$(LD) $(LDFLAGS) -o $@ $^

test_linked: src/linked.c src/unittest.h
	$(CC) $(CFLAGS) -Isrc -DUNIT_TEST $< -o $@

test_dlb: src/dlb.c src/unittest.h
	$(CC) $(CFLAGS) -Isrc -DUNIT_TEST $< -o $@

clean:
	rm -f src/*.o
	rm -f test_ag test_agcore test_dlb test_linked
